package cn.aib.educms.service.impl;

import cn.aib.educms.entity.CrmBanner;
import cn.aib.educms.mapper.CrmBannerMapper;
import cn.aib.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-13
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    @Override
    @Cacheable(key = "'getAllBanner'",value = "banner")
    public List<CrmBanner> getAllBanner() {

        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();

        //以创建时间降序排序
        wrapper.orderByDesc("gmt_create");
        //取前4条
        wrapper.last("limit 0,4");

        List<CrmBanner> crmBanners = baseMapper.selectList(wrapper);

        return crmBanners;
    }

    @Override
    @CacheEvict(key = "'getAllBanner'",value = "banner",allEntries = true)
    public void saveBanner(CrmBanner banner) {
        baseMapper.insert(banner);
    }

    @Override
    @CacheEvict(key = "'getAllBanner'",value = "banner",allEntries = true)
    public void removeBanner(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    @CacheEvict(key = "'getAllBanner'",value = "banner",allEntries = true)
    public void updateBanner(CrmBanner banner) {
        baseMapper.updateById(banner);
    }
}
